﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace TA.DotNetUtils
{
	public static class _XmlNodeExtensions
	{
		public static XmlElement AppendElement(this XmlNode This, string name, string innerText)
		{
			XmlElement xe = This.AppendElement(name);
			xe.InnerText = innerText;
			return xe;
		}
		public static XmlElement AppendElement(this XmlNode This, string name)
		{
			XmlElement xe = (This is XmlDocument ? ((XmlDocument)This) : This.OwnerDocument).CreateElement(name);
			This.AppendChild(xe);
			return xe;
		}
		public static XmlElement AppendClone(this XmlNode This, XmlElement source)
		{
			XmlElement xe = This.AppendElement(source.Name);
			xe.InnerXml = source.InnerXml;
			foreach (XmlAttribute xa in source.Attributes)
			{
				XmlAttribute xaNew = xe.OwnerDocument.CreateAttribute(xa.Name);
				xaNew.Value = xa.Value;
				xe.Attributes.Append(xaNew);
			}
			return xe;
		}
		public static XmlAttribute AppendAttribute(this XmlNode This, string name, string value)
		{
			XmlAttribute xa = (This is XmlDocument ? ((XmlDocument)This) : This.OwnerDocument).CreateAttribute(name);
			xa.Value = value;
			This.Attributes.Append(xa);
			return xa;
		}
	}
}
